<?php
namespace App\Http\Controllers\ApiAdmin;

use App\Service\Admin\CateService;
use Illuminate\Http\Request;

class CateController extends BaseController
{
    //分类

    public function __construct(CateService $service)
    {
        $this->db = $service;
    }

    //列表
    public function getIndex(Request $request)
    {
        $pages['start'] = $request->post('page', 1);
        $pages['limit'] = $request->post('size', 10);
        $pas['type'] = $request->post('type', 0);
        if (!$pas['type']) {
            return $this->toJson(-2, '缺少参数');
        }
        $res = $this->db->getIndex($pages, $pas);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data'], $res['total']);
    }

    //修改
    public function modify(Request $request)
    {
        $id = $request->post('id', 0);
        $pas['type'] = $request->post('type', 0);
        $pas['title'] = $request->post('title', '');
        $pas['icon'] = $request->post('img', '');
        $pas['price'] = $request->post('price', 0);
        $pas['price'] = $pas['price'] * 100;
        if (!$pas['type'] || !$pas['title']) {
            return $this->toJson(-2, '参数错误');
        }
        if ($id) {
            $res = $this->db->modify($id, $pas);
        } else {
            $res = $this->db->add($pas);
        }
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }

    //删除
    public function del(Request $request)
    {
        $id = $request->post('id', 0);
        $res = $this->db->del($id);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }
}